/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package cn.ekuma.impexp.model;

import cn.ekuma.impexp.*;
import com.openbravo.data.loader.DataWriteUtils;
import com.openbravo.data.loader.PreparedSentence;
import com.openbravo.data.loader.SentenceExec;
import com.openbravo.data.loader.SentenceFind;
import com.openbravo.data.loader.SentenceList;
import com.openbravo.data.loader.SerializerRead;
import com.openbravo.data.loader.SerializerWrite;
import com.openbravo.data.loader.TableDefinition;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author Administrator
 */
public abstract class AbstractImpExpTableDefinition {
     protected TableDefinition baseTable;
     int[] filterIndexs;
     Map<Integer ,FieldConverse>  filedConversions;//用与定义对应字段的转换类
     Map<Integer,FieldGenerator> fieldGenerators;//对应字段的值发生器，只对插入有效。

     static int STATE_Default=0;
     static int STATE_Imported=1;
     static int STATE_Exported=2;
     int state;
     List<ImpExpRowDefinition> datas;

    public AbstractImpExpTableDefinition(TableDefinition baseTable) {
        this.baseTable = baseTable;
        filedConversions=new HashMap<Integer ,FieldConverse>();
        fieldGenerators=new HashMap<Integer,FieldGenerator>();
        filterIndexs=new int[]{};
        datas=new ArrayList<ImpExpRowDefinition>();
    }

     public  ImpExpRowDefinition getRow(int rowIndex){
         return  datas.get(rowIndex);
     }
      
      public abstract Object[] getData(int rowIndex);

      public abstract int[] getFieldIndexs();

      public abstract int[] getUpdateFieldIndexs();


     public SerializerRead getSerializerReadBasic() {
        return baseTable.getSerializerReadBasic(getListFieldIndexs());
    }
    public SerializerWrite getSerializerInsertBasic() {
        return baseTable.getSerializerInsertBasic(getListFieldIndexs());
    }
    public SerializerWrite getSerializerDeleteBasic() {
        return baseTable.getSerializerDeleteBasic();
    }
    public SerializerWrite getSerializerUpdateBasic() {
     //return baseTable.getSerializerUpdateBasic(getUpdateFieldIndexs());
       return baseTable.getSerializerUpdateBasic(getListFieldIndexs());
    }

     public SentenceExec getInsertSentence() {
        return getInsertSentence(getListFieldIndexs());
    }

    public SentenceExec getInsertSentence(int[] fieldindx) {
        return new PreparedSentence(baseTable.getM_s(), getInsertSQL(), getSerializerInsertBasic(), null);
    }

     public SentenceList getListSentence() {
        return getListSentence(getSerializerReadBasic());
    }

    public SentenceList getListSentence(SerializerRead sr) {
        return new PreparedSentence(baseTable.getM_s() ,getListSQL(), null,  sr);
    }

    public SentenceExec getDeleteSentence() {
        return getDeleteSentence(getSerializerDeleteBasic());
    }

    public SentenceExec getDeleteSentence(SerializerWrite sw) {
        return new PreparedSentence(baseTable.getM_s(), getDeleteSQL(), sw, null);
    }

     public SentenceExec getUpdateSentence() {
        //return getUpdateSentence(getUpdateFieldIndexs());
    	 return getUpdateSentence(getListFieldIndexs());
    }

    public SentenceExec getUpdateSentence(int[] fieldindx) {
        return new PreparedSentence(baseTable.getM_s(), getUpdateSQL(), getSerializerUpdateBasic(), null);
    }


    public String getListSQL(){
           return baseTable.getListSQL(getListFieldIndexs());
    }

    public String getListSQL(Date start,Date end){
           String sql=getListSQL();
           String dateField=baseTable.getDateFieldName();   
           if(dateField!=null){
        	   sql+=" where ";
        	   if(start!=null&&end!=null){
        		   sql+=baseTable.getDateFieldName()+">="+DataWriteUtils.getSQLValue(start);
                   sql+=" and "+baseTable.getDateFieldName()+"<="+DataWriteUtils.getSQLValue(end);
        	   }else if(start!=null){
        		   sql+=baseTable.getDateFieldName()+">="+DataWriteUtils.getSQLValue(start);
        	   }else if(end!=null){
        		   sql+=baseTable.getDateFieldName()+"<="+DataWriteUtils.getSQLValue(end);
        	   }else
        		   sql+="(1=1)";
           }
           return sql;
    }

    public int[] getListFieldIndexs(){
        return getFieldIndexs().length==0?baseTable.getAllFields():getFieldIndexs();
 }


    public  String getDeleteSQL(){
           return baseTable.getDeleteSQL();
       }

    public String getInsertSQL(){
        return baseTable.getInsertSQL(getListFieldIndexs());
    }

    public  String getUpdateSQL(){
          // return baseTable.getUpdateSQL(getUpdateFieldIndexs());
    	return baseTable.getUpdateWhoutIgnoreSQL(getListFieldIndexs());
   }

    public TableDefinition getBaseTable() {
        return baseTable;
    }

    public int getRowCount(){
    	return datas.size();
    }
    
    public Map<Integer, FieldGenerator> getFieldGenerators() {
        return fieldGenerators;
    }

    public Map<Integer, FieldConverse> getFiledConversions() {
        return filedConversions;
    }

    public int[] getFilterIndexs() {
        return filterIndexs;
    }
    
    public int getState() {
        return state;
    }


    public void addRow(ImpExpRowDefinition row){
        datas.add(row);
    }

	public SentenceFind getExistSentence() {
		return baseTable.getExistSentence(baseTable.getSerializerExistBasic());
	}
      
}
